package com.css.dzj.dzjap.modules.construnit.service.impl;

import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.modules.construnit.entity.ConstructionUnitEntity;
import com.css.dzj.dzjap.modules.construnit.entity.QConstructionUnitEntity;
import com.css.dzj.dzjap.modules.construnit.mapper.ConstrunitMapper;
import com.css.dzj.dzjap.modules.construnit.service.ConstrunitService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * 建设单位servive实现类
 * @author liyunyue
 */
@Service
public class ConstrunitServiceImpl implements ConstrunitService {

    @Autowired
    private ConstrunitMapper construnitMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 判断统一社会信用代码是否已存在
     * @param uniscid
     * @return
     */
    @Override
    public boolean isExistEntCode(String uniscid) {
        QConstructionUnitEntity qConstructionUnitEntity = QConstructionUnitEntity.constructionUnitEntity;
        ConstructionUnitEntity construct = jpaQueryFactory.selectFrom(qConstructionUnitEntity).where(qConstructionUnitEntity.uniscid.eq(uniscid),
                qConstructionUnitEntity.delFlag.eq(Constant.NO_DELETE)).fetchOne();
        if(construct != null) {
            return true;
        }
        return false;
    }

    /**
     * 判断用户名是否已存在
     * @param userName
     * @return
     */
    @Override
    public boolean isExistUserName(String userName) {
        QConstructionUnitEntity qConstructionUnitEntity = QConstructionUnitEntity.constructionUnitEntity;
        ConstructionUnitEntity construct = jpaQueryFactory.selectFrom(qConstructionUnitEntity).where(qConstructionUnitEntity.userName.eq(userName),
                qConstructionUnitEntity.delFlag.eq(Constant.NO_DELETE)).fetchOne();
        if(construct != null) {
            return true;
        }
        return false;
    }

    /**
     * 建设单位注册
     * @param construct
     */
    @Transactional
    @Override
    public void regist(ConstructionUnitEntity construct) {
        try {
            construct.setPassword(construct.getPassword());
            construct.setDelFlag(Constant.NO_DELETE);
            construct.setCreateTime(new Date());
            construct.setCreatePerson("");
            construnitMapper.save(construct);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
